<?php
include '../includes/common.php';
$param=base64_decode($_GET['param']);
$arr=explode("|",authcode($param,'DECODE','authguaos!!'));
$proid=$arr[0];
$version=$arr[1];
$url=$arr[2];
$authcode=$arr[3];
if(!$proid || !$url || !$authcode){
    sysmsg("更新包下载错误，更新失败",2,'/',true);
}
$program=$DB->get_row("SELECT * FROM authguao_program WHERE id='$proid' limit 1");
if($program){
    if($conf['active_download']==1){
        if($program['active']==1){
            sysmsg("该程序已停止运行！",2,'/',true);
        }
    }else{
         if($arr[4]<TIMESTAMP){
             sysmsg("链接已过期，请返回重新下载！",2,'/',true);
         }
        if(checkauth($proid,$url,$authcode)) {
        } else {
            sysmsg("暂时不能获取更新！",2,'/',true);
        }
        $row=$DB->get_row("SELECT * FROM authguao_site WHERE proid='$proid' and url='$url' and authcode='$authcode' limit 1");
        if(!$row){
            $row = $DB->get_row("SELECT * FROM authguao_site WHERE proid='$proid' and url='*.".get_host($url)."' and authcode='$authcode' limit 1");
        }
        if($row){
            $sign=$row['sign'];
require_once(SYSTEM_ROOT.'pclzip.php');
                $file_real = substr($authcode,0,16).'.zip';
                $file = ROOT.CACHE_DIR."/{$file_real}";
                
                $file_path = ROOT.PACKAGE_DIR.'/'.$program['catalog'].'/update6000/';
                    $file_str = file_get_contents(ROOT.PACKAGE_DIR.'/authcode.php');
                    $file_str = str_replace('1000000001',$authcode,$file_str);
                    file_put_contents($file_path.$program['authfile'],$file_str);
                    $file_name ='update.zip';
                    
                    if(file_exists($file))unlink($file);
                $zip = new PclZip($file);
                $v_list = $zip->create($file_path ,PCLZIP_OPT_REMOVE_PATH,$file_path);
                $file_size = filesize("$file");
                header("Content-Description: File Transfer");
                header("Content-Type:application/force-download");
                header("Content-Length: {$file_size}");
                header("Content-Disposition:attachment; filename={$file_name}");
                readfile("$file");
                
        }else{
            sysmsg("该程序不存在该授权！",2,'/',true);
        }
    }
}else{
    sysmsg("该程序不存在！",2,'/',true);
}
?>
